<!-- 表行 》 子列表 -->

<template>
  <el-table-column v-bind="tableColumn">
    <template #header>
      <slot name="label">{{ label }}</slot>
    </template>

    <template #default="{ row }">
      <div v-if="multiRow" :class="getClass(row)">
        <div v-for="(item, i) in getValue(row, bind)" :key="i">
          <slot v-if="hasPrefix" name="prefix" :item="item">{{ prefix }}</slot>
          <slot name="item" :item="item" :text="transName(item)">{{ transName(item) }}</slot>
          <slot v-if="hasSuffix" name="suffix" :item="item">{{ suffix }}</slot>
        </div>
      </div>

      <div v-else :class="getClass(row)">
        <slot v-if="hasPrefix" :row="row" name="prefix">{{ prefix }}</slot>
        <slot :row="row" :text="trans(getValue(row, bind))">{{ trans(getValue(row, bind)) }}</slot>
        <slot v-if="hasSuffix" :row="row" name="suffix">{{ suffix }}</slot>
      </div>
    </template>
  </el-table-column>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import mixRow from '../mixRow'

const name = 'RowChildren'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name,

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [mixRow],

  /**
   * 属性注册 (抽取以便查阅)
   */
  props: CORE.WEB[name],

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 转换名称
     */
    transName(item) {
      if (CORE.isString(item)) return item || ''

      const { nameKey, getValue } = this
      const name = getValue(item, nameKey)
      return CORE.isNumber(name) ? name : (name || '')
    },

    /**
     * 转换
     */
    trans(children) {
      if (!CORE.isArray(children)) return ''

      const nameList = children.map((child) => this.transName(child))
      const and = this.multiRow ? ' \n' : this.and
      return nameList.join(and)
    },

    /**
     * 获取样式类
     */
    getClass(row) {
      const { bind, parentWeb, className, multiRow, diyClass } = this
      const children = this.getValue(row, bind) || []
      const sizeStr = parentWeb.size ? ` ${className}-${parentWeb.size}` : ''
      const multiStr = multiRow ? ` ${className}-multi-row` : ''
      const diyStr = diyClass ? ` ${diyClass}` : ''
      return `CoreWeb-RowContent ${className} ${className}-${children.length}${sizeStr}${multiStr}${diyStr}`
    },
  },
}
</script>
